/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.tabbar.provider;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;

import com.xuexiang.xuidemo.ResourceTable;

import java.util.List;

/**
 * TabBarProvider
 *
 * @since 2021-04-26
 */
public class TabBarProvider extends BaseItemProvider {
    private List<TabBarProvider.DataItem> list;

    /**
     * TabBarProvider
     *
     * @param list
     */
    public TabBarProvider(List<DataItem> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        if (list != null && position >= 0 && position < list.size()) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        ViewHolder viewHolder = null;
        Component cpt = component;
        if (cpt == null) {
            cpt = LayoutScatter.getInstance(componentContainer.getContext()).
                parse(ResourceTable.Layout_item_tabbar, null, false);
            viewHolder = new ViewHolder();
            viewHolder.nameText = (Text) cpt.findComponentById(ResourceTable.Id_name);
            viewHolder.subNameText = (Text) cpt.findComponentById(ResourceTable.Id_sub_name);
            cpt.setTag(viewHolder);
        } else {
            if (cpt.getTag() instanceof ViewHolder) {
                viewHolder = (ViewHolder) cpt.getTag();
            }
        }
        TabBarProvider.DataItem sampleItem = list.get(position);
        if (viewHolder != null) {
            viewHolder.nameText.setText(sampleItem.getName());
            viewHolder.subNameText.setText(sampleItem.getSubName());
        }
        return cpt;
    }

    private static class ViewHolder {
        public Text nameText;
        public Text subNameText;

    }

    public static class DataItem {
        String name;
        String subName;
        String sliceName;

        /**
         * DataItem
         *
         * @param name
         * @param subName
         */
        public DataItem(String name, String subName, String sliceName) {
            this.name = name;
            this.subName = subName;
            this.sliceName = sliceName;
        }

        public String getName() {
            return name;
        }

        public String getSubName() {
            return subName;
        }

        public String getSliceName() {
            return sliceName;
        }
    }
}
